// AWTFlowLayout.cpp: implementation of the AWTFlowLayout class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWT.h"
#include "AWTFlowLayout.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTFlowLayout, AWTLayout)

AWTFlowLayout::AWTFlowLayout()
{
	construct(CENTER, 5, 5);
}

AWTFlowLayout::AWTFlowLayout(int align)
{
	construct(align, 5, 5);
}

AWTFlowLayout::AWTFlowLayout(int align, int hgap, int vgap)	
{
	construct(align, hgap, vgap);
}
	
AWTFlowLayout::~AWTFlowLayout()
{
}

void AWTFlowLayout::moveComponents(AWTContainer* target, int x, int y, int width, int height, int rowStart, int rowEnd)
{
	switch (m_align) 
	{
		case LEFT:
	    break;
		case CENTER:
	    x += width / 2;
	    break;
		case RIGHT:
	    x += width;
	    break;
	}
	for (int i = rowStart ; i < rowEnd ; i++) 
	{
    AWTComponent* m = target->getComponent(i);
    if (m->isVisible()) 
		{
			m->setLocation(x, y + (height - m->getHeight()) / 2);
			x += m_hgap + m->getWidth();
	  }
	}
}

//interface AWTLayoutManager

void AWTFlowLayout::addLayoutComponent(CString* name, AWTComponent* comp)
{
}

void AWTFlowLayout::removeLayoutComponent(AWTComponent* comp)
{
}

CSize AWTFlowLayout::preferredLayoutSize(AWTContainer* target)
{
	CSize dim(0,0);
	int nmembers = target->getComponentCount();

	for (int i = 0 ; i < nmembers ; i++) 
	{
		AWTComponent* m = target->getComponent(i);
		if (m->isVisible()) 
		{
			CSize d = m->getPreferredSize();
			dim.cy = max(dim.cy, d.cy);
			if (i > 0) 
			{
		    dim.cx += m_hgap;
			}
			dim.cx += d.cx;
		}
	}
	AWTInsets insets = target->getInsets();
	dim.cx += insets.left + insets.right + m_hgap*2;
	dim.cy += insets.top + insets.bottom + m_vgap*2;
	return dim;
}

CSize AWTFlowLayout::minimumLayoutSize(AWTContainer* target)
{
	CSize dim(0,0);
	int nmembers = target->getComponentCount();

	for (int i = 0 ; i < nmembers ; i++) 
	{
		AWTComponent* m = target->getComponent(i);
		if (m->isVisible()) 
		{
			CSize d = m->getMinimumSize();
			dim.cy = max(dim.cy, d.cy);
			if (i > 0) 
			{
		    dim.cx += m_hgap;
			}
			dim.cx += d.cx;
		}
	}
	AWTInsets insets = target->getInsets();
	dim.cx += insets.left + insets.right + m_hgap*2;
	dim.cy += insets.top + insets.bottom + m_vgap*2;
	return dim;
}

void AWTFlowLayout::layoutContainer(AWTContainer* target)
{
	AWTInsets insets = target->getInsets();
	int maxwidth = target->getWidth() - (insets.left + insets.right + m_hgap*2);
	int nmembers = target->getComponentCount();
	int x = 0, y = insets.top + m_vgap;
	int rowh = 0, start = 0;

	for (int i = 0 ; i < nmembers ; i++) 
	{
		AWTComponent* m = target->getComponent(i);
	  if (m->isVisible()) 
		{
			CSize d = m->getPreferredSize();
			m->setSize(d.cx, d.cy);
	
			if ((x == 0) || ((x + d.cx) <= maxwidth)) 
			{
				if (x > 0) 
				{
					x += m_hgap;
		    }
		    x += d.cx;
		    rowh = max(rowh, d.cy);
			} 
			else 
			{
		    moveComponents(target, insets.left + m_hgap, y, maxwidth - x, rowh, start, i);
		    x = d.cx;
		    y += m_vgap + rowh;
		    rowh = d.cy;
		    start = i;
			}
		}
	}
	moveComponents(target, insets.left + m_hgap, y, maxwidth - x, rowh, start, nmembers);
}


